﻿using System.Text;

namespace WpfAppMissData.Biz
{
   public class Crc
    {
    public  static string MonitorCrc(string databuffa)
        {
            byte[] databuff = Encoding.Default.GetBytes(databuffa);

            int wkg = 0xFFFF;

            for (int j = 0; j < databuff.Length; j++)
            {

                wkg = (wkg >> 8) & 0x00FF;
                wkg ^= databuff[j];

                for (byte i = 0; i < 8; i++)
                {
                    if ((wkg & 0x0001) != 0)
                    {
                        wkg = (wkg >> 1) ^ 0xa001;
                    }
                    else {
                        wkg = wkg >> 1;
                    }
                }
            }
            string resultString = wkg.ToString("X4");           
            return resultString;
        }

    }
}
